/**
 * 字段检测器
 * 自动检测后端返回的字段名并生成映射
 */

/**
 * 检测地址相关字段的映射关系
 * @param {Object} sampleData - 样本数据
 * @returns {Object} - 字段映射关系
 */
export function detectAddressFields(sampleData) {
  if (!sampleData || typeof sampleData !== 'object') {
    return null
  }
  
  const fields = Object.keys(sampleData)
  const mapping = {}
  
  // 检测收货人姓名字段
  const nameFields = ['receiverName', 'receiver_name', 'name', 'receiverName', 'receiverName']
  mapping.receiver_name = nameFields.find(field => fields.includes(field) && sampleData[field])
  
  // 检测收货人电话字段
  const phoneFields = ['receiverPhone', 'receiver_phone', 'phone', 'mobile', 'telephone', 'tel']
  mapping.receiver_phone = phoneFields.find(field => fields.includes(field) && sampleData[field])
  
  // 检测省份字段
  const provinceFields = ['province', 'provinceName', 'province_name']
  mapping.province = provinceFields.find(field => fields.includes(field) && sampleData[field])
  
  // 检测城市字段
  const cityFields = ['city', 'cityName', 'city_name']
  mapping.city = cityFields.find(field => fields.includes(field) && sampleData[field])
  
  // 检测区县字段
  const districtFields = ['district', 'area', 'districtName', 'areaName']
  mapping.district = districtFields.find(field => fields.includes(field) && sampleData[field])
  
  // 检测详细地址字段
  const streetFields = ['street', 'address', 'detailAddress', 'detail_address', 'fullAddress']
  mapping.street = streetFields.find(field => fields.includes(field) && sampleData[field])
  
  // 检测是否默认字段
  const defaultFields = ['isDefault', 'is_default', 'defaultFlag', 'default', 'isDefaultAddress']
  mapping.is_default = defaultFields.find(field => fields.includes(field))
  
  // 检测用户ID字段
  const userIdFields = ['userId', 'user_id', 'userId', 'userId']
  mapping.user_id = userIdFields.find(field => fields.includes(field) && sampleData[field])
  
  // 检测地址ID字段
  const idFields = ['id', 'addressId', 'address_id']
  mapping.id = idFields.find(field => fields.includes(field) && sampleData[field])
  
  return mapping
}

/**
 * 根据检测到的映射关系适配数据
 * @param {Object} rawData - 原始数据
 * @param {Object} fieldMapping - 字段映射关系
 * @returns {Object} - 适配后的数据
 */
export function adaptDataWithMapping(rawData, fieldMapping) {
  if (!rawData || !fieldMapping) return rawData
  
  const adapted = {
    id: fieldMapping.id ? rawData[fieldMapping.id] : rawData.id,
    receiver_name: fieldMapping.receiver_name ? rawData[fieldMapping.receiver_name] : '',
    receiver_phone: fieldMapping.receiver_phone ? rawData[fieldMapping.receiver_phone] : '',
    province: fieldMapping.province ? rawData[fieldMapping.province] : '',
    city: fieldMapping.city ? rawData[fieldMapping.city] : '',
    district: fieldMapping.district ? rawData[fieldMapping.district] : '',
    street: fieldMapping.street ? rawData[fieldMapping.street] : '',
    is_default: fieldMapping.is_default ? rawData[fieldMapping.is_default] : false,
    user_id: fieldMapping.user_id ? rawData[fieldMapping.user_id] : '',
    _raw: rawData
  }
  
  // 处理布尔值转换
  if (typeof adapted.is_default === 'string') {
    adapted.is_default = adapted.is_default === '1' || adapted.is_default === 'true'
  }
  
  // 处理数字转换
  if (typeof adapted.id === 'string') {
    adapted.id = parseInt(adapted.id) || 0
  }
  if (typeof adapted.user_id === 'string') {
    adapted.user_id = parseInt(adapted.user_id) || 0
  }
  
  return adapted
}

/**
 * 调试：打印字段检测结果
 * @param {Object} sampleData - 样本数据
 * @param {Object} fieldMapping - 检测到的映射关系
 */
export function debugFieldDetection(sampleData, fieldMapping) {
  console.log('🔍 字段检测结果:')
  console.log('原始字段:', Object.keys(sampleData))
  console.log('检测到的映射:', fieldMapping)
  
  if (fieldMapping) {
    console.log('映射详情:')
    Object.entries(fieldMapping).forEach(([targetField, sourceField]) => {
      if (sourceField) {
        console.log(`  ${targetField} <- ${sourceField}: ${sampleData[sourceField]}`)
      } else {
        console.log(`  ${targetField} <- 未找到对应字段`)
      }
    })
  }
} 